﻿using linghaoDal;
using linghaoModel.ApiRequest;
using linghaoModel.Entitys;

namespace linghaoBll
{
    public class WebTableBll
    {
        public DbFunction<WebTable> dbFunction = new DbFunction<WebTable>();
        public async Task<string> Insert(WebTableRequest insertWebRequest)
        { 
            #region 校验
            if (insertWebRequest == null)
            {
                return "请输入数据";
            }
            if (insertWebRequest.WebTypeId == null || insertWebRequest.WebTypeId <= 0)
            {
                return "请输入网站分类ID";
            }
            if (string.IsNullOrEmpty(insertWebRequest.WebType))
            {
                return "请输入网站分类";
            }
            if (string.IsNullOrEmpty(insertWebRequest.WebDesc))
            {
                return "请输入网站介绍";
            }
            if (string.IsNullOrEmpty(insertWebRequest.WebOrApp))
            {
                return "请选择网站类型";
            }
            else if (insertWebRequest.WebOrApp != "应用" && insertWebRequest.WebOrApp != "网站")
            {
                return "网站类型选择错误";
            }
            if (string.IsNullOrEmpty(insertWebRequest.WebUrl))
            {
                return "请输入网站地址";
            }
            #endregion

            var webTable = new WebTable()
            {
                WebType = insertWebRequest.WebType,
                WebTypeId = insertWebRequest.WebTypeId,
                WebOrApp = insertWebRequest.WebOrApp,
                WebDesc = insertWebRequest.WebDesc,
                WebUrl = insertWebRequest.WebUrl,
                CreateTime = DateTime.Now
            };
            // 这里可以调用数据库操作方法进行插入
            string sql = "INSERT INTO WebTable (WebTypeId, WebType, WebOrApp, WebDesc, WebUrl, CreateTime) " +
                         "VALUES (@WebTypeId, @WebType, @WebOrApp, @WebDesc, @WebUrl, @CreateTime)";
            await dbFunction.ExecuteAsync(sql, webTable);
            return Ok();
        }

        public async Task<IEnumerable<WebTable>> Select(string? webtype, string? webdesc)
        {
            string sql = "SELECT * FROM WebTable where 1=1";
            if (!string.IsNullOrEmpty(webtype))
            {
                sql += " and WebType=@webtype";
            }
            if (!string.IsNullOrEmpty(webdesc))
            {
                sql += $" and WebDesc like '%{webdesc}%'";
            }
            return await dbFunction.QueryAsync(sql, new { webtype = webtype });
        }

        public async Task<string> Delete(int Id)
        {
            if (Id <= 0)
            {
                return "未获取到网站信息";
            }
            await dbFunction.ExecuteAsync("DELETE FROM WebTable WHERE Id = @Id", new { Id });
            return Ok();
        }

        public async Task<string> Update(WebTableRequest updateWebRequest)
        {
            #region 校验
            if (updateWebRequest == null)
            {
                return "请输入数据";
            }
            if (updateWebRequest.WebTypeId == null || updateWebRequest.WebTypeId <= 0)
            {
                return "请输入网站分类ID";
            }
            if (string.IsNullOrEmpty(updateWebRequest.WebType))
            {
                return "请输入网站分类";
            }
            if (string.IsNullOrEmpty(updateWebRequest.WebDesc))
            {
                return "请输入网站介绍";
            }
            if (string.IsNullOrEmpty(updateWebRequest.WebOrApp))
            {
                return "请选择网站类型";
            }
            else if (updateWebRequest.WebOrApp != "应用" && updateWebRequest.WebOrApp != "网站")
            {
                return "网站类型选择错误";
            }
            if (string.IsNullOrEmpty(updateWebRequest.WebUrl))
            {
                return "请输入网站地址";
            }
            #endregion

            var webTable = new WebTable()
            {
                Id = updateWebRequest.Id ?? 0,
                WebType = updateWebRequest.WebType,
                WebTypeId = updateWebRequest.WebTypeId,
                WebOrApp = updateWebRequest.WebOrApp,
                WebDesc = updateWebRequest.WebDesc,
                WebUrl = updateWebRequest.WebUrl
            };
            // 这里可以调用数据库操作方法进行插入
            var sql = "UPDATE WebTable SET WebTypeId = @WebTypeId, WebType = @WebType, " +
                          "WebOrApp = @WebOrApp, WebDesc = @WebDesc, WebUrl = @WebUrl" +
                          "WHERE Id = @Id";
            await dbFunction.ExecuteAsync(sql, webTable);
            return Ok();
        }

        public string Ok()
        {
            return "操作成功";
        }
    }
}
